<?php
/*
  $Program: ECJC Online Shop
  $Id: account_manage.php
  $Based on: osCommerce, Open Source E-Commerce Solutions
  Released under the GNU General Public License
*/
  defined( 'joscom' ) or die( 'Direct Access to this location is not allowed.' );
  if (defined( '_OSCOMMERCE_LOADED' )) {
    if (!DB_SHARED) {
      tep_db_connect() or die('Unable to connect to database server!');
    }
    global $category_links, $languages_id, $catID, $catID_array, $current_category_id;
    global $request_type, $lng, $cart, $currencies, $language, $navigation, $breadcrumb;
  } else {
    require('components/'.$option.'/includes/application_top.php');
  }
  require(DIR_WS_FUNCTIONS.'product_manage.php');
  require(DIR_WS_CLASSES.'product_manage.php');
  require(DIR_WS_CLASSES . 'upload.php');
  require(DIR_WS_LANGUAGES . $language . '/account_manage.php');
  function tep_copy_uploaded_file($filename, $target) {
	if (substr($target, -1) != '/') $target .= '/';

	$target .= $filename['name'];
//die($filename['tmp_name'].$target);

	move_uploaded_file($filename['tmp_name'], $target);
}
function tep_get_uploaded_file($filename) {
	if (isset($_FILES[$filename])) {
		$uploaded_file = array('name' => $_FILES[$filename]['name'],
		'type' => $_FILES[$filename]['type'],
		'size' => $_FILES[$filename]['size'],
		'tmp_name' => $_FILES[$filename]['tmp_name']);
	} elseif (isset($GLOBALS['HTTP_POST_FILES'][$filename])) {
		global $HTTP_POST_FILES;

		$uploaded_file = array('name' => $HTTP_POST_FILES[$filename]['name'],
		'type' => $HTTP_POST_FILES[$filename]['type'],
		'size' => $HTTP_POST_FILES[$filename]['size'],
		'tmp_name' => $HTTP_POST_FILES[$filename]['tmp_name']);
	} else {
		$uploaded_file = array('name' => $GLOBALS[$filename . '_name'],
		'type' => $GLOBALS[$filename . '_type'],
		'size' => $GLOBALS[$filename . '_size'],
		'tmp_name' => $GLOBALS[$filename]);
	}

return $uploaded_file;
}

  define (FILENAME_MANAGE,"account_manage.php");
  $customer_query = tep_db_query("select customers_firstname,customers_lastname,customers_email_address,customers_ap from " . TABLE_CUSTOMERS . " where customers_id = '" . (int)$customer_id . "'");
  $customer = tep_db_fetch_array($customer_query);
global $my;
if ($my->id != '62') {
  if (!$customer['customers_ap']) {
     echo "<script>alert('Administrator must apporve your account for this function'); window.history.go(-1);</script>\n";
     exit();
     }
     }
  $currencies = new currencies();

  $action = (isset($_REQUEST['action']) ? $_REQUEST['action'] : '');
  $category_id  = (isset($_REQUEST['category_id']) ? $_REQUEST['category_id'] : '');

  if (tep_not_null($action)) {
    switch ($action) {
      case 'delete_product_confirm':
        if (isset($_POST['products_id']) && isset($_POST['product_categories']) && is_array($_POST['product_categories'])) {
          $product_id = tep_db_prepare_input($_POST['products_id']);
          $product_categories = $_POST['product_categories'];

          for ($i=0, $n=sizeof($product_categories); $i<$n; $i++) {
            tep_db_query("delete from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . (int)$product_id . "' and categories_id = '" . (int)$product_categories[$i] . "'");
          }

          $product_categories_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . (int)$product_id . "'");
          $product_categories = tep_db_fetch_array($product_categories_query);

          if ($product_categories['total'] == '0') {
            tep_remove_product($product_id);
          }
        }

        if (USE_CACHE == 'true') {
          tep_reset_cache_block('categories');
          tep_reset_cache_block('also_purchased');
        }

        tep_redirect(tep_href_link('account_manage.php', 'option=com_oscommerce&osMod=account_manage&catID=' . $catID));
        break;

         case 'new_product_preview':

         $products_image = $_FILES['products_image']['name'];
        $file = tep_get_uploaded_file('products_image');
		if (is_uploaded_file($file['tmp_name'])) {
		//die(DIR_WS_IMAGES);
		
			tep_copy_uploaded_file($file, DIR_FS_IMAGES);
		}
        
        if ($products_image) {
        $products_image_name = $_FILES['products_image']['name'];
        //$usrfl_name = $_FILES['usrfl']['name'];
       // die ($products_image);
        }else{
        $products_image_name = (isset($_POST['products_previous_image']) ? $_POST['products_previous_image'] : '');
      //  die ('oops');
        }
        break;

   case 'insert_product':
   case 'product_update':
        if (isset($_POST['edit_x']) || isset($_POST['edit_y'])) {
          $action = 'new_product';
        } else {
          if (isset($_REQUEST['pID'])) $products_id = tep_db_prepare_input($_REQUEST['pID']);
          $products_date_available = tep_db_prepare_input($_POST['products_date_available']);

          $products_date_available = (date('Y-m-d') < $products_date_available) ? $products_date_available : 'null';

          $sql_data_array = array('products_quantity' => tep_db_prepare_input($_POST['products_quantity']),
                                  'products_model' => tep_db_prepare_input($_POST['products_model']),
                                  'products_price' => tep_db_prepare_input($_POST['products_price']),
                                  'products_date_available' => $products_date_available,
                                  'products_weight' => tep_db_prepare_input($_POST['products_weight']),
                                  'products_length' => tep_db_prepare_input($_POST['products_length']),
                                  'products_depth' => tep_db_prepare_input($_POST['products_depth']),
                                  'products_width' => tep_db_prepare_input($_POST['products_width']),
                                  'products_status' => '0',//tep_db_prepare_input($_POST['products_status']),
                                  'products_tax_class_id' => tep_db_prepare_input($_POST['products_tax_class_id']),
                                  'customer_id' => $customer_id,
                                  'manufacturers_id' => tep_db_prepare_input($_POST['manufacturers_id']));

          if (isset($_POST['products_image']) && tep_not_null($_POST['products_image']) && ($_POST['products_image'] != 'none')) {
            $sql_data_array['products_image'] = tep_db_prepare_input($_POST['products_image']);
          }

          if ($action == 'insert_product') {
            $insert_sql_data = array('products_date_added' => 'now()');

            $sql_data_array = array_merge($sql_data_array, $insert_sql_data);

            tep_db_perform(TABLE_PRODUCTS, $sql_data_array);
            $products_id = tep_db_insert_id();

            tep_db_query("insert into " . TABLE_PRODUCTS_TO_CATEGORIES . " (products_id, categories_id) values ('" . (int)$products_id . "', '" . (int)$category_id . "')");
          } elseif ($action == 'product_update') {
            $update_sql_data = array('products_last_modified' => 'now()');

            $sql_data_array = array_merge($sql_data_array, $update_sql_data);

            tep_db_perform(TABLE_PRODUCTS, $sql_data_array, 'update', "products_id = '" . (int)$products_id . "'");

            $sql_data_array = array(' categories_id' => tep_db_prepare_input($_POST['category_id']) );
            tep_db_perform(TABLE_PRODUCTS_TO_CATEGORIES, $sql_data_array, 'update', "products_id = '" . (int)$products_id . "'");
          }

          $languages = tep_get_languages();
          for ($i=0, $n=sizeof($languages); $i<$n; $i++) {
            $language_id = $languages[$i]['id'];

            $sql_data_array = array('products_name' => tep_db_prepare_input($_POST['products_name'][$language_id]),
                                    'products_description' => tep_db_prepare_input($_POST['products_description'][$language_id]),
                                    'products_url' => tep_db_prepare_input($_POST['products_url'][$language_id]));

            if ($action == 'insert_product') {
              $insert_sql_data = array('products_id' => $products_id,
                                       'language_id' => $language_id);

              $sql_data_array = array_merge($sql_data_array, $insert_sql_data);

              tep_db_perform(TABLE_PRODUCTS_DESCRIPTION, $sql_data_array);
            } elseif ($action == 'product_update') {

              tep_db_perform(TABLE_PRODUCTS_DESCRIPTION, $sql_data_array, 'update', "products_id = '" . (int)$products_id . "' and language_id = '" . (int)$language_id . "'");
            }
          }

          if (USE_CACHE == 'true') {
            tep_reset_cache_block('categories');
            tep_reset_cache_block('also_purchased');
          }
          define(EMAIL_TEXT_SUBJECT,'New Product was added/changed');
          $category_query = tep_db_query("select categories_name from " . TABLE_CATEGORIES_DESCRIPTION . " where categories_id = '" . (int)$category_id . "' and language_id = '" . (int)$language_id ."'");
          $category = tep_db_fetch_array($category_query);

          $email =  "Hello administrator\n\n";
          $email .= "New product was added/changed to : ".$category['categories_name']."\n\n";
          $email .= "Product was added by : ". $customer['customers_firstname'].' '.$customer['customers_lastname'] ."\n\n";
          $email .= "For approve new product go to administrator panel \n\n";
          tep_mail(STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS, EMAIL_TEXT_SUBJECT, $email,$customer['customers_firstname'].' '.$customer['customers_lastname'],$customer['customers_email_address']);
          tep_redirect(tep_href_link('account_manage.php', 'ecjcID=' . $ecjcID . '&pID=' . $products_id));
        }
        break;
    }
  }


// check if the catalog image directory exists
  if (is_dir(DIR_FS_CATALOG_IMAGES)) {
    if (!is_writeable(DIR_FS_CATALOG_IMAGES)) $messageStack->add(ERROR_CATALOG_IMAGE_DIRECTORY_NOT_WRITEABLE, 'error');
  } else {
    $messageStack->add(ERROR_CATALOG_IMAGE_DIRECTORY_DOES_NOT_EXIST, 'error');
  }
       $breadcrumb->add('My Account', tep_href_link(FILENAME_ACCOUNT, '', 'SSL'));
  $breadcrumb->add(HEADING_TITLE_BREADCRUMB , tep_href_link('account_manage.php', '', 'SSL'));
      
require(WS_TEMPLATES . E_TEMPLATE_FOLDER . '/'.$osMod.'.html');
require(DIR_WS_INCLUDES . 'footer.php'); 
require(DIR_WS_INCLUDES . 'application_bottom.php');

  // -- webMS Modification --
  // reset the Mambo Database connection (stomped by osCommerce Connection)
  if (!DB_SHARED) {
    global $database, $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix;
    $database = new database( $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix );
  }
 ?>
